package com.hitqz.robot.biz.flow.component.agv;

import cn.hutool.json.JSONObject;
import com.google.common.collect.Maps;
import com.hitqz.robot.biz.flow.domain.constant.FlowConstant;
import com.hitqz.robot.biz.flow.util.LiteFlowUtil;
import com.hitqz.robot.biz.service.RobotService;
import com.yomahub.liteflow.annotation.LiteflowComponent;
import com.yomahub.liteflow.core.NodeComponent;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.client.RestTemplate;

import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author xupkun
 * @date 2024/11/6
 */
@Slf4j
@LiteflowComponent(value = "WriteVarName", name = "[轮对/轮轴]写入数据变量")
@AllArgsConstructor
public class WriteVarName extends NodeComponent {

    private final RobotService robotService;

    private final RestTemplate restTemplate;

    private static final String uri = "http://127.0.0.1:8900/api/fms/writeVariable";

    @Override
    public void process() throws Exception {
        LiteFlowUtil.checkInterruptFlag();
        String key = FlowConstant.NODE_CUSTOM_INFO_MAP.get(this.getNodeId()).getParamNames().get(0);
        String varName = this.getCmpData(JSONObject.class).get(key, String.class);
        String varType = this.getCmpData(JSONObject.class).get(FlowConstant.NODE_CUSTOM_INFO_MAP.get(this.getNodeId()).getParamNames().get(1), String.class);
        String varValue = this.getCmpData(JSONObject.class).get(FlowConstant.NODE_CUSTOM_INFO_MAP.get(this.getNodeId()).getParamNames().get(2), String.class);
        Map<String, Object> requestParams = Maps.newHashMap();
        requestParams.put("VehicleNo", robotService.getDefaultRobot().getRobotName());
        requestParams.put("VariableName", varName);
        requestParams.put("VariableType", varType);
        requestParams.put("VariableValue", varValue);
        String requestBody = com.alibaba.fastjson.JSONObject.toJSONString(requestParams);
        while (true) {
            String result = sendDispatch(requestBody, uri);
            log.info("写入变量结果 {}--{}--{}--请求数据:{}-- 结果：{} ", varName, varType, varValue,
                    requestBody, result);
            JSONObject jsonObject = new JSONObject(result);
            if (jsonObject.containsKey("Result")) {
                if (jsonObject.getBool("Result")) {
                    log.info("变量写入成功");
                    break;
                }
            }
            TimeUnit.SECONDS.sleep(1);
        }
        log.info("写入变量成功{}--{}--{}", varName, varType, varValue);
    }


    private String sendDispatch(String requestBody, String uri) {
        try {
            RestTemplate restTemplate = new RestTemplate();
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_JSON);
            HttpEntity<String> requestEntity = new HttpEntity<>( requestBody, headers);
            return restTemplate.postForObject(uri, requestEntity, String.class);
        } catch (Exception e) {
            log.info("请求地址发生异常:" + e.getMessage());
        }
        return null;
    }


}
